package com.zany.util.quartz.service;

import java.util.ArrayList;
import java.util.List;

import com.zany.system.vo.ParameterSingleVO;
import com.zany.util.dao.SingleParamUtilDao;
import com.zany.util.data.DataUtils;
import com.zany.util.date.DateFormater;
import com.zany.util.param.SysParamKey;
import com.zany.util.quartz.dao.JobDao;
import com.zany.util.quartz.vo.AccountCheckVO;
import com.zany.util.quartz.vo.CalculateVO;

public class JobServiceImpl implements JobService{

	private JobDao jobDao;
	private SingleParamUtilDao singleParamUtilDao;
	
	public CalculateVO getStartDate(CalculateVO vo) {
		// TODO Auto-generated method stub
		return jobDao.getStartDate(vo);
	}
	public void updateFund(CalculateVO vo, Double rate, String endDate) {
		// TODO Auto-generated method stub
		List<CalculateVO> doorList = new ArrayList<CalculateVO>();
		vo.setEndDate(endDate);
		doorList = jobDao.getDoorMoneyList(vo);
		jobDao.updateBalanceTemp();
		for(CalculateVO doorVO:doorList){
			vo.setStartDate(doorVO.getMoneyDate());
			Double doorMoney = 0d;
			Double costMoney = 0d;
			Double repairMoney = 0d;
			Double money = 0d;
			doorMoney = doorVO.getMoney()*DateFormater.dateSubtractionStr(vo.getEndDate(), vo.getStartDate());
			doorVO.setStartDate(vo.getStartDate());
			doorVO.setEndDate(vo.getEndDate());
			List<CalculateVO> costList = new ArrayList<CalculateVO>();
			costList = jobDao.getCostMoney(doorVO);
			for(CalculateVO costVO:costList){
				if(costVO !=null){
					costMoney += costVO.getMoney()*DateFormater.dateSubtractionStr(costVO.getMoneyDate(), vo.getStartDate());
				}
			}
			List<CalculateVO> repairList = new ArrayList<CalculateVO>();
			repairList = jobDao.getRepairMoney(doorVO);
			for(CalculateVO repairVO:repairList){
				if(repairVO !=null){
					repairMoney += repairVO.getMoney()*DateFormater.dateSubtractionStr(repairVO.getMoneyDate(), vo.getStartDate());
				}
			}	
			money = DataUtils.doubleScale2((doorMoney - costMoney + repairMoney)*rate/360);
			doorVO.setMoney(doorVO.getMoney() + money);
			doorVO.setRateTotal(money);
			jobDao.updateCalculate(doorVO);
		}
		ParameterSingleVO paramVO = new ParameterSingleVO();
		paramVO.setParamCode("RATE1");
		paramVO.setParamVal(endDate);
		jobDao.updateRateDate(paramVO);
		jobDao.deleteRateBalance(paramVO);
		jobDao.insertRateBalance(paramVO);
	}
	
	public JobDao getJobDao() {
		return jobDao;
	}
	public void setJobDao(JobDao jobDao) {
		this.jobDao = jobDao;
	}
	public SingleParamUtilDao getSingleParamUtilDao() {
		return singleParamUtilDao;
	}
	public void setSingleParamUtilDao(SingleParamUtilDao singleParamUtilDao) {
		this.singleParamUtilDao = singleParamUtilDao;
	}
	
	public ParameterSingleVO getParameterSingleDetail(ParameterSingleVO vo) {
		// TODO Auto-generated method stub
		return jobDao.getParameterSingleDetail(vo);
	}
	public void insertAccountCheck(List<AccountCheckVO> list) {
		// TODO Auto-generated method stub
		jobDao.deleteAccountCheck();
		for(AccountCheckVO vo:list){
			jobDao.insertAccountCheck(vo);
		}
	}
	public void checkBankAccount(List<AccountCheckVO> list) {
		// TODO Auto-generated method stub
		for(AccountCheckVO vo:list){
			AccountCheckVO accountCheckVO = new AccountCheckVO();
			if("1".equals(vo.getCostType())){
				accountCheckVO = jobDao.getCheckCost(vo);
				if(accountCheckVO == null){
					vo.setCostSts("1");
					jobDao.insertCost(vo);
					jobDao.updateDoorMoney(vo);
				}else{
					jobDao.updateCostCheckSts(vo);
				}
			}else{
				List<AccountCheckVO> batchList = new ArrayList<AccountCheckVO>();			
				batchList = jobDao.getBatchCostListForSts(vo);
				if(batchList.size() != 0){
					for(AccountCheckVO batchVO:batchList){
						batchVO.setCostSts("4");
						batchVO.setBankSerial(vo.getBankSerial());
						batchVO.setCostDate(vo.getCostDate());
						jobDao.insertCost(batchVO);
						jobDao.updateDoorMoney(batchVO);						
					}
					jobDao.updateBatchSts(vo);
				}else{
					jobDao.updateCostCheckSts(vo);
				}				
			}
		}
	}
	
	@Override
	public void setFundRest() {
		// TODO Auto-generated method stub
		jobDao.setFundRest();
	}
	@Override
	public void updateRate(CalculateVO calculateVO) {
		// TODO Auto-generated method stub
		//获取去年本日计息数据  
		List<CalculateVO> preList = new ArrayList<CalculateVO>();
		ParameterSingleVO paramVO = new ParameterSingleVO();
		paramVO.setParamCode(SysParamKey.RATE.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rate= Double.valueOf(paramVO.getParamVal());
		
		paramVO.setParamCode(SysParamKey.RATEMONTH1.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rateMonth1 =  Double.valueOf(paramVO.getParamVal());
		
		paramVO.setParamCode(SysParamKey.RATEMONTH2.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rateMonth2 = Double.valueOf(paramVO.getParamVal());
		
		paramVO.setParamCode(SysParamKey.RATEYEAR1.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rateYear1 = Double.valueOf(paramVO.getParamVal());
		
		paramVO.setParamCode(SysParamKey.RATEYEAR2.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rateYear2 = Double.valueOf(paramVO.getParamVal());
		
		paramVO.setParamCode(SysParamKey.RATEYEAR3.getKey());
		paramVO = jobDao.getParameterSingleDetail(paramVO);
		Double rateYear3 = Double.valueOf(paramVO.getParamVal());
		
		preList = jobDao.getPreRateList(calculateVO);
		for(CalculateVO vo:preList){
//			Double rate = 0d;
//			if("RATE1".equals(vo.getRateType())){
//				rate = DataUtils.doubleScale2(vo.getBalance()*rate2);
//			}else if("RATE2".equals(vo.getRateType())){
//				rate = DataUtils.doubleScale2(vo.getBalance()*rate3);
//			}else if("RATE3".equals(vo.getRateType())){
//				rate = DataUtils.doubleScale2(vo.getBalance()*rate1);
//			}else{
//				rate = DataUtils.doubleScale2(vo.getBalance()*rate1);
//			}
			vo.setBalanceTemp(vo.getBalance());
			vo.setRateTotal(DataUtils.sumMoney(vo.getRateTotal(), rate));
			vo.setBalance(DataUtils.sumMoney(vo.getBalance(), rate));
			
		}
	}
	@Override
	public void updateFund(Double rate, Double rateYear1, String endDate) {
		// TODO Auto-generated method stub
		//活期
		List<CalculateVO> doorForRateList = new ArrayList<CalculateVO>();
		CalculateVO vo = new CalculateVO();
		vo.setEndDate(endDate);
		doorForRateList = jobDao.getDoorListForRate(vo);
		for(CalculateVO doorRateVO: doorForRateList){
			jobDao.updateBalanceTempForRate(doorRateVO);
			Double rateMoney = 0d;
			rateMoney = DataUtils.doubleScale2(doorRateVO.getBalance()*365*rate/360);
			doorRateVO.setBalance(doorRateVO.getBalance() + rateMoney);
			doorRateVO.setRateTotal(rateMoney);
			doorRateVO.setRateType("RATE_YEAR");
			doorRateVO.setEndDate(endDate);
			jobDao.updateCalculateForHouseId(doorRateVO);
			doorRateVO.setRateType1("RATE_YEAR");
			doorRateVO.setRateType2("RATE1");			
			jobDao.updateGroupRate(doorRateVO);			
		}
		//定期
		List<CalculateVO> doorForRateYear1List = new ArrayList<CalculateVO>();
		vo.setEndDate(endDate);
		doorForRateYear1List = jobDao.getDoorListForRateYear1(vo);
		for(CalculateVO doorRateYearVO: doorForRateYear1List){
			jobDao.updateBalanceTempForRate(doorRateYearVO);
			Double rateMoney = 0d;
			rateMoney = DataUtils.doubleScale2(doorRateYearVO.getBalance()*365*rateYear1/360);
			doorRateYearVO.setBalance(doorRateYearVO.getBalance() + rateMoney);
			doorRateYearVO.setRateTotal(rateMoney);
			doorRateYearVO.setRateType("RATE1");
			doorRateYearVO.setEndDate(endDate);
			jobDao.updateCalculateForHouseId(doorRateYearVO);
			doorRateYearVO.setRateType1("RATE1");
			doorRateYearVO.setRateType2("RATE1");			
			jobDao.updateGroupRate(doorRateYearVO);
		}
		ParameterSingleVO paramVO = new ParameterSingleVO();
		paramVO.setParamVal(endDate);
		jobDao.deleteRateBalance(paramVO);
		jobDao.insertRateBalance(paramVO);
	}
}
